<?php

namespace Api\Event;


/**
 * 应用App接口  for  版本信息
 *
 * @author Legend. <xcx_legender@qq.com>
 * @param unknown
 * @return
 */

class AppVersionEvent extends BaseEvent
{

	/**
	 * 获取最新的版本信息
	 *
	 * @author Legend. <xcx_legender@qq.com>
	 * @param unknown
	 * @return
	 */

	public function getLatest( $type ) {
		if ( !in_array( $type, array_keys( C( 'PHONE_OS' ) ) ) ) {
			return;
		}
		$service = service('AppVersion');
		$map['type']   = $type;
		$map['status'] = $service::STATUS_PASS;
		$res = $service->where( $map )->order( 'internal_version desc' )->field( 'internal_version,md5,url,force_update,outer_version,create_time,log,update_info' )->limit(1)->select();
		end($res);

		return $res ? array_values($res)[0] : '';
	}

	/**
	 * 版本更新信息
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @param  string  $type 客户端版本
	 * @param  integer $ver  内部版本号
	 */
	public function getVersionInfo($type, $ver)
	{
		if (!in_array($type, array_keys(C('PHONE_OS')))) {
			return;
		}
		$service = service('AppVersion');
		$map['type']   = $type;
		$map['internal_version'] = $ver;
		$map['status'] = $service::STATUS_PASS;
		$res = $service->where( $map )->field( 'internal_version,md5,url,force_update,outer_version,create_time,log,update_info' )->find();
		return $res;
	}

	/**
	 * 版本更新记录
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function lists($filter = [], $field = '', $sort = 'internal_version desc', $p = 0, $limit = 0)
	{
		$service = service('AppVersion');
		$map['status'] = $service::STATUS_PASS;
		$map = array_merge($filter, $map);

		$field = $field ?: 'type,internal_version,url,detail,outer_version,create_time,log';
		$query = $service->where($map)->field($field)->order($sort);
		if ($p && $limit) {
			$query->page($p)->limit($limit);
		}
		$res = $query->select();
		return $res ? array_values($res) : [];
	}
}